package com.liuqi.business.controller.admin;


import com.google.common.base.Preconditions;
import com.liuqi.base.BaseAdminController;
import com.liuqi.base.BaseService;
import com.liuqi.business.enums.CurrencyExtractTypeEnum;
import com.liuqi.business.enums.ProtocolEnum;
import com.liuqi.business.enums.SwitchEnum;
import com.liuqi.business.model.CurrencyExtractModel;
import com.liuqi.business.model.CurrencyExtractModelDto;
import com.liuqi.business.model.CurrencyModelDto;
import com.liuqi.business.service.CurrencyExtractService;
import com.liuqi.business.service.CurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/admin/currencyExtract")
public class AdminCurrencyExtractController extends BaseAdminController<CurrencyExtractModel,CurrencyExtractModelDto> {

    @Autowired
    private CurrencyExtractService currencyExtractService;
    //jsp基础路径
    private final static String JSP_BASE_PTH="admin";
    //模块
    private final static String BASE_MODUEL="currencyExtract";
    //默认为""表示可以使用add和update。  重写add或update时，原有方法禁止使用 NOT_OPERATE="add,update"
    private final static String NOT_OPERATE="";
    @Override
    public BaseService getBaseService() {
    	return this.currencyExtractService;
    }

    @Override
    public String getJspBasePath() {
        return JSP_BASE_PTH;
    }
    @Override
    public String getBaseModuel() {
        return BASE_MODUEL;
    }
    @Override
    public String getNotOperate() { return NOT_OPERATE;}
    @Override
    public String getDefaultExportName() { return DEFAULT_EXPORTNAME;}
    /*******待修改  排序  导出**********************************************************************************************************/
    //默认导出名称
    private final static String DEFAULT_EXPORTNAME="提现配置";
    @Override
    public String[] getDefaultExportHeaders(){
        String[] headers = {"主键","创建时间","更新时间","备注","版本号","币种","协议","充提名称","提币开关（0关 1开）","类型0百分比 1固定数量","提币手续费","最低提币数量","最高提币数量","提币每天最大数量开关（0关 1开）","提币每天最大数量","排序"};
        return headers;
    }
    @Override
    public String[] getDefaultExportColumns(){
        String[] columns = {"id","createTime","updateTime","remark","version","currencyId","protocol","thirdCurrency","extractSwitch","type","rate","min","max","maxDaySwitch","maxDay","position"};
        return columns;
    }

    @Override
    protected String getNotIncludeField() {
        return super.getNotIncludeField()+",currency_id,protocol,";
    }

    /*******自己代码**********************************************************************************************************/


    @Autowired
    private CurrencyService currencyService;

    @Override
    protected void toListHandle(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toListHandle(modelMap, request, response);
        this.getEnumList(modelMap);
    }

    @Override
    protected void toAddHandle(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toAddHandle(modelMap, request, response);
        this.getEnumList(modelMap);
    }

    @Override
    protected void toUpdateHandle(CurrencyExtractModelDto t, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toUpdateHandle(t, modelMap, request, response);
        t.setCurrencyName(currencyService.getNameById(t.getCurrencyId()));
        t.setProtocolStr(ProtocolEnum.getName(t.getProtocol()));
        this.getEnumList(modelMap);
    }

    @Override
    protected void addHandle(CurrencyExtractModelDto t, Long curUserId, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CurrencyExtractModelDto temp=currencyExtractService.getByCurrencyAndProtocol(t.getCurrencyId(),t.getProtocol());
        Preconditions.checkArgument(temp==null,"已存在数据");
    }

    @Override
    protected void toViewHandle(CurrencyExtractModelDto t,ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toViewHandle(t,modelMap, request, response);
        this.getEnumList(modelMap);
    }

    private void getEnumList(ModelMap modelMap){
        List<CurrencyModelDto> list= currencyService.getAll();
        modelMap.put("currencyList",list);
        modelMap.put("switchList", SwitchEnum.getList());
        modelMap.put("typeList", CurrencyExtractTypeEnum.getList());
        modelMap.put("protocolList", ProtocolEnum.getList());
    }

}
